package com.rcl.himalaya_kotlin.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.rcl.himalaya_kotlin.R
import com.squareup.picasso.Picasso
import com.ximalaya.ting.android.opensdk.model.album.Album
import org.w3c.dom.Text

/*
 * class title: 
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2020/9/3.
 * PS: Not easy to write code, please indicate.
 */
class RecommendAdapter() :
    RecyclerView.Adapter<RecommendAdapter.InnerHolder>() {

    val mData = arrayListOf<Album>()

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): RecommendAdapter.InnerHolder {
        //加载View
        val itemView: View = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_recommend_adapter, parent, false)
        return InnerHolder(itemView)
    }

    override fun onBindViewHolder(holder: RecommendAdapter.InnerHolder, position: Int) {
        holder.itemView.tag = position
        holder.setData(mData[position])
    }

    override fun getItemCount() = mData.size

    public fun setData(albumList: List<Album>) {
        mData.clear()
        mData.addAll(albumList)
        notifyDataSetChanged()
    }

    class InnerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        public fun setData(album: Album) {
            val iv_cover: ImageView = itemView.findViewById(R.id.iv_cover)
            val tv_title: TextView = itemView.findViewById(R.id.tv_title)
            val tv_desc: TextView = itemView.findViewById(R.id.tv_desc)
            val tv_play: TextView = itemView.findViewById(R.id.tv_play)
            val tv_sound: TextView = itemView.findViewById(R.id.tv_sound)

            Picasso.get().load(album.coverUrlLarge).into(iv_cover)
            tv_title.text = album.albumTitle
            tv_desc.text = album.albumIntro
            tv_play.text = "${album.playCount}万"
            tv_sound.text = "${album.includeTrackCount}集"
        }
    }

}